Window: message event

您所在的位置:网站首页 window addeventlistener remove Window: message event

Window: message event

2023-08-07 18:59| 来源: 网络整理| 查看: 265

Suppose a script sends a message to a different browsing context, such as another , using code like this:

js

const targetFrame = window.top.frames[1]; const targetOrigin = "https://example.org"; const windowMessageButton = document.querySelector("#window-message"); windowMessageButton.addEventListener("click", () => { targetFrame.postMessage("hello there", targetOrigin); });

The receiver can listen for the message using addEventListener() with code like this:

js

window.addEventListener("message", (event) => { console.log(`Received message: ${event.data}`); });

Alternatively the listener could use the onmessage event handler property:

js

window.onmessage = (event) => { console.log(`Received message: ${event.data}`); };


【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3